package com.relatev.minecraft.TRMobSpawn;

import com.google.gson.Gson;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.Location;
import org.bukkit.configuration.file.YamlConfiguration;

public class DataManager {

    private static File DataFile;
    private static YamlConfiguration data;
    public static HashMap<Location, TRSpawnPointInfo> SpawnPoints;
    private static Gson gson;

    public static void init() {
        gson = new Gson();
        DataFile = new File(TRMobSpawn.MainPlugin.getDataFolder(), "data.yml");
        if (DataFile.exists() == false) {
            DataFile.getParentFile().mkdirs();
            try {
                DataFile.createNewFile();
            } catch (IOException ex) {
                Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        data = YamlConfiguration.loadConfiguration(DataFile);
        SpawnPoints = new HashMap();
        if (data.contains("SpawnPoints.Locations")) {
            List<Location> locs = (List<Location>) data.getList("SpawnPoints.Locations");
            for (Location loc : locs) {
                SpawnPoints.put(loc, gson.fromJson(data.getString("SpawnPoints.TRMobInfo." + loc), TRSpawnPointInfo.class));
            }
        }
    }

    public static void save() {
        data.set("SpawnPoints.Locations", new ArrayList(SpawnPoints.keySet()));
        for (Location loc : SpawnPoints.keySet()) {
            data.set("SpawnPoints.TRMobInfo." + loc, gson.toJson(SpawnPoints.get(loc)));
        }
        try {
            data.save(DataFile);
        } catch (IOException ex) {
            Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
